<?php

/**
 * This is the model class for table "{{pro_package}}".
 *
 * The followings are the available columns in table '{{pro_package}}':
 * @property integer $zpp_id
 * @property string $zpp_title
 * @property double $zpp_price
 * @property string $zpp_content
 * @property string $zp_ids
 * @property integer $zel_id
 * @property integer $zm_id
 * @property integer $zpp_status
 * @property integer $inputtime
 * @property integer $updatetime
 */
class ProPackage extends CActiveRecord
{
	//套餐产品
	public $products;
	
	//店铺名称，用于筛选
	public $company_name;
	
	//套餐原价
	public $costPrice;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return ProPackage the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{pro_package}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zel_id, zm_id, zpp_status, inputtime, updatetime, zpp_exp_status', 'numerical', 'integerOnly'=>true),
			array('zpp_price', 'numerical'),
			array('zpp_title, company_name', 'length', 'max'=>100),
			array('zpp_content,zp_ids', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zpp_id, zpp_title, zpp_price, zpp_content, zp_ids, zpp_exp_status, zel_id, zm_id, zpp_status, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'company'=>array(self::BELONGS_TO, 'Company', 'zm_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zpp_id' => 'Zpp',
			'zpp_title' => 'Zpp Title',
			'zpp_price' => 'Zpp Price',
			'zpp_content' => 'Zpp Content',
			'zp_ids' => 'Zp Ids',
			'zpp_exp_status' => 'Zpp Exp Status',
			'zel_id' => 'Zel',
			'zm_id' => 'Zm',
			'zpp_status' => 'Zpp Status',
			'inputtime' => 'Inputtime',
			'updatetime' => 'Updatetime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zpp_id',$this->zpp_id);
		$criteria->compare('zpp_title',$this->zpp_title,true);
		$criteria->compare('zpp_price',$this->zpp_price);
		$criteria->compare('zpp_content',$this->zpp_content,true);
		$criteria->compare('zp_ids',$this->zp_ids,true);
		$criteria->compare('zpp_exp_status',$this->zpp_exp_status);
		$criteria->compare('zel_id',$this->zel_id);
		$criteria->compare('zpp_status',$this->zpp_status);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);
		//根据公司名称筛选
		if(!empty($this->company_name)){
			$criteria->join = ' LEFT JOIN {{Company}} c ON c.zm_id=t.zm_id ';
			$criteria->addCondition(' c.zc_name like "%'.$this->company_name.'%" ');
		}else{
			$criteria->compare('zm_id',$this->zm_id);
		}

		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 获取状态
	 */
	public function getZppStatus($type=null){
		if($type==null){
			return array(
				''=>'全部',
				'1'=>'有效',
				'2'=>'失效',
			//	'3'=>'删除',
			);
		}else{
			$level = $this->getZppStatus();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 保存前的操作
	 */
	protected function beforeSave(){
		if($this->isNewRecord){
			$this->zm_id = Company::model()->getCmpUserId();
			$this->zpp_status = 1;//状态有效
			$this->inputtime = time();
		}else{
			$this->updatetime = time();
		}
		$this->zp_ids = join(',',$this->zp_ids);//产品ID多个用逗号隔开
		return true;
	}
	
	/**
	 * 查找后的操作
	 * 关联套餐产品
	 */
	 protected function afterFind(){
	 	$this->zp_ids = explode(',',$this->zp_ids);
	 	//计算套餐原价，实例化套餐商品
	 	$costPrice = 0;
	 	foreach($this->zp_ids as $val){
	 		$product = Product::model()->findByAttributes(array('zp_id'=>$val));
	 		$this->products[] = $product;
	 		$costPrice +=  $product->zp_price;
	 	}
	 	$this->costPrice = $costPrice;
	 	return true;
	 }
	 
	 /**
	  * 获取指定产品关联的套餐
	  * @param $pid int 产品ID
	  */
	  public function getPackages($pid){
		  	$criteria=new CDbCriteria;
		  	$criteria->compare('zpp_status',1);//状态：1有效 2失效 3删除'
		  	$criteria->addCondition(' (zp_ids like "'.$pid.',%") or (zp_ids like "%,'.$pid.',%") or (zp_ids like "%,'.$pid.'")');
		  	$criteria->limit = 5;
		  	$data = $this->findAll($criteria);
		  	//将套餐产品进行排序，当前活跃产品排第一
		  	foreach($data as &$val){
		  		$val->products = $this->ListOrderProducts($val->products,$pid);
		  	}
		  	return $data;
	  }
	 
	  /**
	   * 套餐产品排序,将指定ID产品排套餐最前端
	   * @param $products arrayObject套餐产品
	   * @param $pid int 产品ID
	   */
	  public function ListOrderProducts($products,$pid){
		  	$data = array();
		  	$i = 1;
		  	foreach($products as $val){
		  		if($val->zp_id==$pid){
		  			$data[0] = $val;
		  		}else{
		  			$data[$i] = $val;
		  			$i++;
		  		}
		  	}
		  	ksort($data);
		  	return $data;
	  }
}